import { _decorator, Collider2D, Component, Enum, PhysicsSystem2D, rect, Vec2, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

export enum PhysicsSystemEnum{
    testPoint,
    testAABB,
    raycast,
}Enum(PhysicsSystemEnum);

@ccclass('PhysicalSystemDetection')
export class PhysicalSystemDetection extends Component {
    @property({type:PhysicsSystemEnum , tooltip:"测试类型"}) testType:PhysicsSystemEnum = 0;

    @property({group:{name:"raycast",displayOrder: 0}, tooltip:"方向"}) direction:Vec2 = new Vec2(0,1);
    @property({group:{name:"raycast",displayOrder: 1}, tooltip:"距离"}) distance:number = 100;

    @property({group:{name:"testAABB",displayOrder: 0}, tooltip:"宽高"}) size:Vec2 = new Vec2(100,100);
    @property({group:{name:"testAABB",displayOrder: 1}, tooltip:"位置(左下)"}) position:Vec2 = new Vec2(0,0);

    @property({group:{name:"testPoint",displayOrder: 0}, tooltip:"位置"}) point:Vec2 = new Vec2(0,0);
    @property isOpen = false;

    public contacts:Collider2D[] = [];

    update(deltaTime: number) {
        if(this.isOpen){
           switch(this.testType){
            case PhysicsSystemEnum.testPoint:
                this.testPoint();
                break; 
            case PhysicsSystemEnum.testAABB:
                this.testAABB();
                break;
            case PhysicsSystemEnum.raycast:
                this.raycast();
                break;
            default:break;
        } 
        }
    }

    testPoint(){
        const pos = this.node.worldPosition;
        // console.info("testPoint",pos);
        const a = PhysicsSystem2D.instance.testPoint(new Vec2(pos.x, pos.y));
        let str = "";
        a.forEach((item)=>{
            str += item.name + "\n";
        })
        console.info("tp -> ",str);
        
    }

    testAABB(){
        const pos = this.node.worldPosition;
        // console.info("testPoint",pos);
        const a = PhysicsSystem2D.instance.testAABB(rect(pos.x, pos.y,100,100));
        let str = "";
        a.forEach((item)=>{
            str += item.name + "\n";
        })
        console.info("tab -> ",str);
    }

    raycast(){
        this.contacts = [];
        const pos = this.node.worldPosition;
        // console.info("testPoint",pos); pos.add(new Vec3(0,100,0)
        const a = PhysicsSystem2D.instance.raycast(pos,pos.clone().add(new Vec3(this.distance*this.direction.x,this.distance*this.direction.y,0)));
        let str = "";
        a.forEach((item)=>{
            if(item.collider){
                this.contacts.push(item.collider); 
            }
            // str += item.collider.name + "\n";
        })
        // console.info("rc -> ",str);
    }
}


